package com.sundi.ecmarket.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.dto.IssueDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcBrand;
import com.sundi.ecmarket.entity.EcIssue;
import com.sundi.ecmarket.service.IEcIssueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 常见问题表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/ecIssue")
public class EcIssueController {



    @Autowired
    IEcIssueService iEcIssueService;

    /**
     * @deprecated ec商城中商场管理里的常见问题修改列表查询模块
     * @author 许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueList")
    public ResultEntity list(IssueDto issueDto){

        PageHelper.startPage(issueDto.getPageNo(),issueDto.getPageSize());
        List<EcIssue> list = iEcIssueService.selectIssueList(issueDto);
        PageInfo<EcIssue> pageInfo = new PageInfo(list);
        return ResultEntity.ok(pageInfo);
    }

    /**
     * @deprecated ec商城中商场管理里的常见问题修改逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
        @RequestMapping("/xujiabao/logicdel")
        public ResultEntity logindel(Integer id){

            System.err.println(id);
            iEcIssueService.updateLogicDel(id);
            return ResultEntity.ok();
    }

    /**
     * @deprecated ec商城中商场管理里的常见问题添加模块
     * @author  许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueAdd")
    public ResultEntity add(@RequestBody IssueDto issueDto){
        System.err.println(issueDto);
        EcIssue ecIssue = new EcIssue();
        BeanUtils.copyProperties(issueDto,ecIssue);
        if(ecIssue.getId()==null){
            ecIssue.setAddTime(new Date());
        }
        boolean insert = iEcIssueService.insert(ecIssue);
        return ResultEntity.ok(insert);
    }


    /**
     * @deprecated ec商城中商场管理里的常见问题修改模块
     * @author  许家宝
     * @param issueDto
     * @return
     */
    @RequestMapping("/xujiabao/issueUpd")
    public ResultEntity upd(@RequestBody IssueDto issueDto){
        System.err.println(issueDto);
        EcIssue ecIssue = new EcIssue();
        BeanUtils.copyProperties(issueDto,ecIssue);
        if(ecIssue.getId()!=null){
            ecIssue.setUpdateTime(new Date());
        }
        boolean b = iEcIssueService.updateById(ecIssue);
        return ResultEntity.ok(b);
    }

    @RequestMapping("/yidong")
    @Transactional
    public ResultEntity shang(Integer id){

        EcIssue ecIssue = iEcIssueService.selectById(id);
        Integer id1 = ecIssue.getId();

        EcIssue issue = iEcIssueService.getStuOrder(id);
        Integer id2 = ecIssue.getId();
        issue.setId(id1);
        ecIssue.setId(id2);

        iEcIssueService.updateById(ecIssue);
        iEcIssueService.updateById(issue);

        return ResultEntity.ok();
    }


    @RequestMapping("/xiayidong")
    @Transactional
    public ResultEntity xia(Integer id){

        EcIssue ecIssue = iEcIssueService.selectById(id);
        Integer id1 = ecIssue.getId();

        EcIssue issue = iEcIssueService.getStuXiaOrder(id);
        Integer id2 = issue.getId();
        issue.setId(id1);
        ecIssue.setId(id2);

        iEcIssueService.updateById(ecIssue);
        iEcIssueService.updateById(issue);

        return ResultEntity.ok();
    }
}
